package controllers

import (
	"finance/internal/dto"
	"finance/internal/message"
	"finance/internal/services"
	"finance/internal/utils"
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"net/http"
)

// GetSystemLogs @Summary 获取系统行为日志
// @Description 获取所有用户行为日志（系统操作记录）
// @Tags audit
// @Produce json
// @Success 200 {object} utils.Response{data=[]dto.SystemLogResponse}
// @Failure 500 {object} utils.Response
// @Router /api/audit/system [get]
func GetSystemLogs(c *gin.Context) {
	db := c.MustGet("DB").(*gorm.DB)

	logs, err := services.GetAllSystemLogs(db)
	if err != nil {
		utils.Fail(c, http.StatusInternalServerError, message.GetSystemLogFailed)
		return
	}

	var result []dto.SystemLogResponse
	for _, log := range logs {
		result = append(result, dto.SystemLogResponse{
			ID:        log.ID,
			UserID:    log.UserID,
			Action:    log.Action,
			IP:        log.IP,
			Detail:    log.Detail,
			CreatedAt: log.CreatedAt,
		})
	}

	utils.Success(c, result)
}

// GetAuditLogs @Summary 获取AI分析结果
// @Description 获取AI安全分析的审计结果
// @Tags audit
// @Produce json
// @Success 200 {object} utils.Response{data=[]dto.AuditLogResponse}
// @Failure 500 {object} utils.Response
// @Router /api/audit/result [get]
func GetAuditLogs(c *gin.Context) {
	db := c.MustGet("DB").(*gorm.DB)

	audits, err := services.GetAllAuditLogs(db)
	if err != nil {
		utils.Fail(c, http.StatusInternalServerError, message.GetAIAnalyzeFailed)
		return
	}

	var result []dto.AuditLogResponse
	for _, a := range audits {
		result = append(result, dto.AuditLogResponse{
			ID:        a.ID,
			Date:      a.Date,
			Summary:   a.Summary,
			RiskLevel: a.RiskLevel,
			Details:   a.Details,
			CreatedAt: a.CreatedAt,
		})
	}

	utils.Success(c, result)
}
